package com.xj.admin.bussiness.subscribe.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Controller;
import com.xj.admin.base.index.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.plugins.Page;
import com.feilong.core.Validator;
import com.xj.admin.bussiness.subscribe.service.ITbSubscribeService;
import com.xj.admin.util.JsonUtil;
import com.xj.admin.util.dtgrid.model.Pager;
import com.xj.common.base.common.bean.AbstractBean;
import com.xj.common.base.common.exception.EnumSvrResult;
import com.xj.common.bussiness.subscribe.entity.TbSubscribe;

/**
 * <p>
 *   前端控制器
 * </p>
 *
 * @author CF
 * @since 2019-09-11
 */
@Controller
@RequestMapping("/subscribe/")
public class TbSubscribeController extends BaseController {

	@Autowired
	private ITbSubscribeService subscribeService;
	
	@GetMapping("listUI")
    public String listUI() {
		return "subscribe/list";
    }
	
	@GetMapping("form")
    public String form(Map<String,Object> map) {
		return "subscribe/form";
    }
    
    @SuppressWarnings("unchecked")
	@PostMapping("list")
	@ResponseBody
    public Object list(String gridPager) {
		Pager pager = JsonUtil.getObjectFromJson(gridPager, Pager.class);
		Map<String, Object> parameters = null;
		if(Validator.isNullOrEmpty(pager.getParameters())){
			parameters = new HashMap<>();
		}else{
			parameters = pager.getParameters();
		}
		Page<TbSubscribe> list = subscribeService.selectPage(new Page<TbSubscribe>(pager.getNowPage(), pager.getPageSize()), Condition.create().allEq(parameters));
		List<TbSubscribe> records = list.getRecords();
		for (TbSubscribe tbSubscribe : records) {
			tbSubscribe.setProvince(TbSubscribe.getAdrName(tbSubscribe.getProvince()));
			tbSubscribe.setCity(TbSubscribe.getAdrName(tbSubscribe.getCity()));
			tbSubscribe.setCounty(TbSubscribe.getAdrName(tbSubscribe.getCounty()));
		}
		list.setRecords(records);
		makeGridResult(parameters, pager, list);
		return parameters;
    }
	
	@PostMapping("save")
	@ResponseBody
	public AbstractBean add(TbSubscribe tbsubscribe){
		AbstractBean bean = new AbstractBean();
		if(tbsubscribe.getId()==null){
			tbsubscribe.setCreateTime(new Date());
			tbsubscribe.setCreateUser(getUserEntity().getAccountName());
			tbsubscribe.setUpdateTime(new Date());
		}else{
			tbsubscribe.setUpdateTime(new Date());
			tbsubscribe.setUpdateUser(getUserEntity().getAccountName());
		}
//		tbsubscribe.setIsShow(tbnews.getIsShow()==null?"2":"1");
		if(!subscribeService.insertOrUpdate(tbsubscribe)){
			bean.setStatus(EnumSvrResult.ERROR.getVal());
			bean.setMessage(EnumSvrResult.ERROR.getName());
		}
	return bean;
	}
	
	@DeleteMapping("{id}/delete")
	@ResponseBody
    public AbstractBean delete(@PathVariable(required=true) Integer id) {	
		if(!subscribeService.deleteById(id)){
			return error();
		}
		return success();
    }	
	
	@GetMapping("{id}/select")
    public String select(Map<String,Object> map,@PathVariable(required=true) Integer id) {	
		TbSubscribe tbsubscribe = subscribeService.selectById(id);
		map.put("record", tbsubscribe);
		return "subscribe/edit";
    }	
}
